Given the root of a binary search tree (BST) and an integer target, split the tree into two subtrees where one subtree has nodes that are all smaller or equal to the target value, while the other subtree has all nodes that are greater than the target value. It Is not necessarily the case that the tree contains a node with the value target.
Additionally, most of the structure of the original tree should remain. Formally, for any child c with parent p in the original tree, if they are both in the same subtree after the split, then node c should still have the parent p.
Return an array of the two roots of the two subtrees.
Example 1:
Input: root = [4,2,6,1,3,5,7], target = 2 Output: [[2,1],[4,3,6,null,null,5,7]]
Example 2:
Input: root = [1], target = 1 Output: [[1],[]]
Constraints:
- The number of nodes in the tree is in the range
[1, 50]. 0 <= Node.val, target <= 1000
Average Rating: 3.45 (71 votes)
Approach 1: Recursion
Intuition and Algorithm
The root node either belongs to the first half or the second half. Let's say it belongs to the first half.
Then, because the given tree is a binary search tree (BST), the entire subtree at root.left must be in the first half. However, the subtree at root.right may have nodes in either halves, so it needs to be split.
In the diagram above, the thick lines represent the main child relationships between the nodes, while the thinner colored lines represent the subtrees after the split.
Lets say our secondary answer bns = split(root.right) is the result of such a split. Recall that bns[0] and bns[1] will both be BSTs on either side of the split. The left half of bns must be in the first half, and it must be to the right of root for the first half to remain a BST. The right half of bns is the right half in the final answer.
The diagram above explains how we merge the two halves of split(root.right) with the main tree, and illustrates the line of code root.right = bns[0] in the implementations.
Complexity Analysis
-
Time Complexity: O(N), where N is the number of nodes in the input tree, as each node is checked once.
-
Space Complexity: O(N).
Last Edit: September 17, 2018 3:06 AM
Why is this problem medium? It is so convoluted. If given at an interview, I don't think I can write out clear codes like this.
Last Edit: February 22, 2019 9:02 AM
Here is a potential follow-up: what if it is an exclusive split? e.g. for a BST root=[1,2,3,4,5], V=3, we want to output [[1,2], [4,5]]
BTW i think this question should also be included in the Explore/Learn session, along with other classic operations such as Search, Insert, Delete and Merge.
700. Search in a Binary Search Tree
701. Insert into a Binary Search Tree
450. Delete Node in a BST
617. Merge Two Binary Trees
FYI
Last Edit: October 26, 2018 7:24 PM
I got so much grey hair trying to understand what "The left half of bns must be in the first half, and it must be to the right of root for the first half to remain a BST." means.
time complexity is the height of the tree. at any level, only one node is visited.
Last Edit: October 4, 2018 3:35 AM
Time Complexity: O(N), where N is the number of nodes in the input tree, as each node is checked once.
You could also say O(height), right? Your "each node is checked once" sounds so absolute, like that's always the case, not just in worst cases.
And hmm... the Python specification says rtype: List[TreeNode] and I always understood that as list. But you don't return a list but a tuple. Does "List" mean "any kind of list", not just list, and does tuple count as "List"? That would at least be non-standard terminology.
November 10, 2019 7:28 AM
Solution presented is inversely proportional to explanation.
Brilliant solution. Really poor explanation. Too bad LeetCode! PLEASE PROOF READ YOUR EXPLANATIONS WITH A NATIVE ENGLISH SPEAKER WHO IS (preferably) NOT A SOFTWARE ENGINEER. Then they'll make sense. Then people who use LeetCode will find more value in their subscriptions. It is easy to write code - hard to explain it in a lucid way. The LeetCode solution writers take the easy way out and leave it to the reader to put 2 and 2 together.
June 11, 2019 12:09 PM
I was thinking about in order traverse, but after seeing this one... forget about it LOL
@kenteng, isn't this the reason why we are looking around here, eh? this question and delete a node in BST are both convoluted to me. guess just need to think more and adapt to those questions.
September 3, 2018 3:08 AM
What an elegant solution!
You don't have any submissions yet.
xxxxxxxxxx/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */class Solution {public: vector<TreeNode*> splitBST(TreeNode* root, int target) { }};